package com.yksj.healthtalk.ui.home;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.chatting.ChatActivity;

/**
 * 多美助理
 * @author zhao
 *
 */
public class HelperListActivity extends BaseFragmentActivity implements OnClickListener{
	TextView mTextView;
	final BroadcastReceiver mReceiver = new BroadcastReceiver(){
		public void onReceive(android.content.Context context, Intent intent) {
			String id = intent.getStringExtra("senderId");
			if(SmartFoxClient.helperId.equals(id)){
				onUpdateNumber();
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.helper_list_layout);
		initView();
	}
	
	private void initView(){
		initTitle();
		
		findViewById(R.id.item).setOnClickListener(this);
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("我的助理");
		
		mTextView = (TextView)findViewById(R.id.item_dele);
		TextView textView  = (TextView)findViewById(R.id.name);
		textView.setText("多美助理");
		
		ImageView imageView  = (ImageView)findViewById(R.id.head_image);
		imageView.setImageResource(R.drawable.icon_healper);
		imageView =  (ImageView)findViewById(R.id.head_sex);
		LevelListDrawable drawable  = (LevelListDrawable)imageView.getDrawable();
		drawable.setLevel(2);
		
		textView  = (TextView)findViewById(R.id.note);
		textView.setText("我是多美助理");
		
		
	}
	
	private void onUpdateNumber(){
		List<MessageEntity> list = HTalkApplication.getAppData().messageCllection.get(SmartFoxClient.helperId);
		if(list != null){
			mTextView.setVisibility(View.VISIBLE);
			mTextView.setText(String.valueOf(list.size()));
		}else{
			mTextView.setVisibility(View.INVISIBLE);
		}
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		IntentFilter filter = new IntentFilter(MessagePushService.ACTION_MESSAGE);
		registerReceiver(mReceiver, filter);
		
		onUpdateNumber();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(mReceiver);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.item:
			CustomerInfoEntity entity = HTalkApplication.getAppData().getCustomerInfoEntity(SmartFoxClient.helperId);
			Intent intent = new Intent(this,ChatActivity.class);
			intent.putExtra(ChatActivity.KEY_PARAME,entity);
			startActivity(intent);
			break;
		}
	}
	
}
